<template>
  <div class="app-container">
    <el-tabs type="border-card" @tab-click="handleClick" v-model="activeName">
      <el-tab-pane label="职位管理" name="first">
        <el-form :model="position" label-width="90px" label-position="left" inline>
          <el-form-item label="名称" prop="name" style="width: 300px">
            <el-input v-model="position.name" placeholder="职位名称"/>
          </el-form-item>
          <el-button type="primary" @click="handleAddPosition">添加</el-button>
        </el-form>

        <el-table :data="positionList" style="width: 100%;margin-top:30px;" border>
          <el-table-column align="center" prop="name" label="名称">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="danger" size="small" @click="handleDeletePosition(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="等级管理" name="second">
        <el-form :model="level" label-width="90px" label-position="left" inline>
          <el-form-item label="等级" prop="level" style="width: 300px">
            <el-input v-model="level.level" placeholder="等级"/>
          </el-form-item>
          <el-button type="primary" @click="handleAddLevel">添加</el-button>
        </el-form>

        <el-table :data="levelList" style="width: 100%;margin-top:30px;" border>
          <el-table-column align="center" prop="name" label="名称" >
            <template slot-scope="scope">
              {{ scope.row.level }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="danger" size="small" @click="handleDeleteLevel(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getPositions, addPosition, deletePosition } from '@/api/position'
import { addLevel, deleteLevel, getLevels } from '@/api/level'

const defaultPosition = {
  id: '',
  name: '',
}
const defaultLevel = {
  id: '',
  level: '',
}
export default {
  name: 'PositionManager',
  comments: {},
  created() {
    // Mock: get all routes and Positions list from server
    this.getPositions()
    this.getLevels()
  },
  data() {
    return {
      position: Object.assign({}, defaultPosition),
      level: Object.assign({}, defaultLevel),
      positionList: [],
      levelList: [],
      checkStrictly: false,
      activeName: 'first',
    }
  },
  methods: {
    async getPositions() {
      const res = await getPositions()
      this.positionList = res.data
    },
    async getLevels() {
      const res = await getLevels()
      this.levelList = res.data;
    },
    async handleAddPosition() {
      await addPosition(this.position.name)
      await this.getPositions()
      this.$notify({
        title: 'Success',
        dangerouslyUseHTMLString: true,
        message: "操作成功",
        type: 'success'
      })
    },
    async handleAddLevel() {
      await addLevel(this.level.level)
      await this.getLevels()
      this.$notify({
        title: 'Success',
        dangerouslyUseHTMLString: true,
        message: "操作成功",
        type: 'success'
      })
    },
    handleDeletePosition({ $index, row }) {
      this.$confirm('确定要删除这个职位吗?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deletePosition(row.id, 'FALSE')
          this.positionList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    handleDeleteLevel({ $index, row }) {
      this.$confirm('确定要删除这个等級吗?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteLevel(row.id, 'FALSE')
          this.levelList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    handleClick() {

    }
  }
}
</script>

<style scoped>

</style>
